<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public $com;
    public $jobs;
	public function __construct(){
		parent::__construct();
		$this->com = D('Companys as a');
        $this->jobs = D('Jobs'); 
	}
    public function index(){
    	//检测登陆并检测COOKIE是否正确
    	if(cookie('Qkey')){
    		if(!homechcekck()){
			$this->error('信息不正确,请重新登陆!','/');
    			exit;
    		}
        }else{
        	//未登陆状态动作待写
    	}
    	$cat = D('Admin/cate')->getTree();
    	$this->assign('cat',$cat);
		$link=D('Friendlink');
		$this->assign('links',$link->select());
         
        //热门职业
        $hotjob =D('jobs as c')->join('v5_companys as a ON a.com_id=c.com_id')->field('c.department,c.positionaddress,c.salarymin,c.salarymax,c.workyear,c.education,c.positionadvantage,c.pubtime,c.com_id,c.job_id')->where('c.is_hot=1')->order('c.job_id desc')->limit(0,5)->select();
        //var_dump($hotjob);exit;
        $this->assign('hotjob',$hotjob);
        //最新职业
        $newjob =D('jobs as c')->join('v5_companys as a ON a.com_id=c.com_id')->field('c.department,c.positionaddress,c.salarymin,c.salarymax,c.workyear,c.education,c.positionadvantage,c.pubtime,c.com_id,c.job_id')->order('c.job_id desc')->limit(0,5)->select();
        //var_dump($newjob);exit;
        $this->assign('newjob',$newjob);
        
        //热门公司
        $hotcom=D('companys as a')->join('v5_com_creater as b ON a.com_id=b.com_id')->join('v5_jobs as c ON a.com_id=c.com_id')->field('a.comname,a.com_ly,b.cre_name,a.com_sta,a.com_size,a.com_tag,c.com_id,c.job_id')->where('c.is_hot=1')->order('c.job_id desc')->limit(0,5)->select();
        //var_dump($hotcom);exit;
        $this->assign('hotcom',$hotcom);
        //最新公司
        $newcom=D('companys as a')->join('v5_com_creater as b ON a.com_id=b.com_id')->join('v5_jobs as c ON a.com_id=c.com_id')->field('a.comname,a.com_ly,b.cre_name,a.com_sta,a.com_size,a.com_tag,c.com_id,c.job_id')->order('c.job_id desc')->limit(0,5)->select();
        //var_dump($newcom);exit;
        $this->assign('newcom',$newcom);
    
    	$this->display();

    }

    public function companylist(){
        $com = $this->com->field('com_id,com_sname,com_ly,com_city,com_sta,com_tag')->select();
        foreach ($com as $v=>$c) {
            $c['jobs'] = $this->jobs->where(['com_id'=>$c['com_id']])->limit(0,3)->field('job_id,positionname')->select();
            $com[$v] = $c;
        }

        $job = $this->jobs->field('com_id,positionName')->select();
        $this->assign('job',$job);
    	$this->assign('comlist',$com);

    	$this->display('Index/companylist');
    }

    public function jobview(){
        if(IS_POST){    
            //检测登陆并检测COOKIE是否正确
            if(cookie('Qkey')){
                if(!homechcekck()){
                $this->error('信息不正确,请重新登陆!','/');
                    exit;
                }
            }else{
                $this->error('您还未登陆呢,请登陆后再试!','/home/user/login',1);
            }
            //查出用户简历ID
            $uresid = D('Postresumes')->where(['uid'=>I('post.uid')])->field('pid')->find()['pid'];
            if($uresid === NULL){
                exit($this->error('您还没填写简历吧!请填写简历!','/Home/Jianli/Jianli',1));
            }
            //实例化简历投递表
            $respost =  D('Resumes_post');
            
            //判断用户是否投递过简历
            $ckpost = $respost->where(['job_id'=>$_POST['job_id'],'status'=>'0'])->field('job_id')->select();
           
            //判断是否找到信息,如果找到则证明有投递
            //初步验证通过,等待后续多次验证...
            if($ckpost){
                exit($this->error('这份工作您已经投递过简历了!请等待公司处理!','javascript:history.back(-1);',1));
            }
    
            $respost->uid = $_POST['uid'];
            $respost->pid = $uresid;
            $respost->job_id = $_POST['job_id'];
            $respost->com_id = $_POST['com_id'];
            $respost->positionName = $_POST['positionname'];
            $respost->posttime = time();
            $res = $respost->add();
            if($res){
                exit($this->success('投递成功!','javascript:history.back(-1);',1));
            }else{
                exit($this->error('投递失败!','javascript:history.back(-1);',1));
            }

        }else{
            $id = I('get.jid');
            $job = $this->jobs->find($id);
            if ($job===null) {
               $this->error('你找的工作不存在','/Home/index/companylist',3);
            }else{
                $com = $this->com->find($job['com_id']);
                $rz   = D('Com_rz')->where(['com_id'=>$com['com_id']])->select();
                $this->assign('rz',$rz);
                $this->assign('com',$com);
                $this->assign('job',$job);
                $this->display('Index/jobdetail');
            }
        }
        
    }

    public function comview(){
        $id  =  I('get.id');
        $com = $this->com->where(['com_id'=>$id])->find();
        $prod = D('Com_prod')->where(['com_id'=>$id])->select();
        $create = D('Com_creater')->where(['com_id'=>$id])->select();
        $job = $this->jobs->where(['com_id'=>$id])->limit(0,5)->select();
        $this->assign('job',$job);
        $this->assign('cre',$create);
        $this->assign('prod',$prod);
        $this->assign('com',$com);
        $this->display('Company/index04');
    }

    public function joblist(){
        if(isset($_GET['kd'])){
            //有参数输出搜索结果
            $keywords = I('get.kd');
            $map['positionName'] =array('like',array("%$keywords%","%$keywords","$keywords%"),'OR');
            $count  = D('Jobs as job')->where($map)->join('v5_companys as com ON job.com_id = com.com_id')->count();
            $Page = new \Think\Page($count,2);  
            $show = $Page->show();
            $res  = D('Jobs as job')->where($map)->join('v5_companys as com ON job.com_id = com.com_id')->field('job.workyear,job.pubtime,job.job_id,job.com_id,job.positionname,job.salarymin,job.salarymax,job.education,job.positionadvantage,job.positionaddress,com.com_sname,com.com_ly,com.com_size,com.com_sta,com.com_tag')->limit($Page->firstRow.','.$Page->listRows)->select();
          
            //var_dump($res);exit;
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->assign('res',$res);
            $this->display('Index/list');
        }elseif(isset($_GET['address'])){
            $address = I('get.address');
            $map['positionAddress'] =array('like',array("%$address%","%$address","$address%"),'OR'); 
            $count  = D('Jobs as job')->where($map)->join('v5_companys as com ON job.com_id = com.com_id')->count();
            $Page = new \Think\Page($count,2);  
            $show = $Page->show();
            $res  = D('Jobs as job')->where($map)->join('v5_companys as com ON job.com_id = com.com_id')->field('job.workyear,job.pubtime,job.job_id,job.com_id,job.positionname,job.salarymin,job.salarymax,job.education,job.positionadvantage,job.positionaddress,com.com_sname,com.com_ly,com.com_size,com.com_sta,com.com_tag')->limit($Page->firstRow.','.$Page->listRows)->select();
          
            //var_dump($res);exit;
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->assign('res',$res);
            $this->displIay('Index/list');
        }else{
            //无参数或参数时候输出所有职位
            $count  = D('Jobs as job')->join('v5_companys as com ON job.com_id = com.com_id')->count();
            $Page = new \Think\Page($count,2);  
            $show = $Page->show();
            $res  = D('Jobs as job')->join('v5_companys as com ON job.com_id = com.com_id')->field('job.workyear,job.pubtime,job.job_id,job.com_id,job.positionname,job.salarymin,job.salarymax,job.education,job.positionadvantage,job.positionaddress,com.com_sname,com.com_ly,com.com_size,com.com_sta,com.com_tag')->limit($Page->firstRow.','.$Page->listRows)->select();
          
            //var_dump($res);exit;
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->assign('res',$res);
            $this->display('Index/list');
        }
    }


}
